﻿using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using xab;

namespace libx
{


    public enum NameBy
    {
        /// <summary>
        /// 明确指定AB包名, 文件夹下匹配到的所有文件都用一个名字
        /// </summary>
        Explicit,
        /// <summary>
        /// 每个文件都用自己的路径作为AB包名
        /// </summary>
        Path,
        /// <summary>
        /// 文件夹下匹配到的所有文件都用文件的父文件夹作为AB包名, 有几个子文件夹就会有几个包名
        /// </summary>
        Directory,
        /// <summary>
        /// 匹配文件夹作为AB包名
        /// </summary>
        TopDirectory,
        /// <summary>
        /// 使用文件名作为AB包名(重名的文件会被打到同一个Ab包)
        /// </summary>
        FileName,

        //RelativeFileName,
        //RelativeDirectory,
    }


    [Serializable]
    public class BundleRule
    {
        public bool enable = true;

        public string searchPath;

        public string searchPattern;

        public SearchOption searchOption = SearchOption.AllDirectories;

        public NameBy nameBy = NameBy.Path;

        public string assetBundleName;

        public CompressType compressType = CompressType.LZ4;

        /// <summary>
        /// Bundle名字替换规则
        /// </summary>
        public string replaceRegex = "";
        /// <summary>
        /// Bundle名字替换成什么字符串
        /// </summary>
        public string replaceStr = "";


        /// <summary>
        /// 某条规则产生的所有Bunde包(仅用于展示, 序列化到缓存中, 重启unity还能看到上次的结果)
        /// </summary>
        [NonSerialized]
        public List<BundleInfo> bundleInfos = new List<BundleInfo>();

        /// <summary>
        /// 规则匹配到的所有资源的路径, fbx, anim, dll, cs, meta等不会被包含在里面
        /// </summary>
        public string[] GetMatchAssets()
        {
            var patterns = this.searchPattern.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (!Directory.Exists(searchPath))
            {
                Debug.LogWarning($"Rule searchPath not exist: {searchPath}");
                return new string[0];
            }

            var getFiles = new List<string>();
            foreach (var onePattern in patterns)
            {
                var files = Directory.GetFiles(this.searchPath, onePattern, this.searchOption);
                foreach (var file in files)
                {
                    if (Directory.Exists(file)) //文件夹
                        continue;

                    var ext = Path.GetExtension(file).ToLower();
                    if ((ext == ".fbx" || ext == ".anim") && !onePattern.Contains(ext))
                        continue; //仅仅是会被引用的?所以忽略?

                    if (!Utils.ValidateAsset(file))
                        continue;

                    var assetPath = file.Replace("\\", "/");
                    getFiles.Add(assetPath);
                }
            }

            return getFiles.ToArray();
        }

        /// <summary>
        /// 添加规则产生的AB包
        /// </summary>
        /// <param name="info"></param>
        public void AddBundleInfo(BundleInfo info)
        {
            if (!bundleInfos.Contains(info))
            {
                bundleInfos.Add(info);
            }
        }

    } //end of class


}
